#/***************************************************************************\
# * Copyright (C) by Keio University
# * macros.cmake created in 10 2011.
# * Mail : fdesorbi@hvrl.ics.keio.ac.jp
# *
# * macros.cmake is part of the HVRL Engine Library.
# *
# * The HVRL Engine Library is free software; you can redistribute it and/or modify
# * it under the terms of the GNU Lesser General Public License as published by
# * the Free Software Foundation; either version 3 of the License, or
# * (at your option) any later version.
# *
# * The HVRL Engine Library is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# * GNU Lesser General Public License for more details.
# *
# * You should have received a copy of the GNU Lesser General Public License
# * along with this program.  If not, see <http://www.gnu.org/licenses/>.
# *
# ***************************************************************************/


macro(create_test name)

SET(HVRL_INCLUDE_DIRS ${HVRL_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/include" CACHE INTERNAL "")

	PROJECT(HVRL_TEST_${name})

	SET(the_target "hvrl_${name}")

	INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include" "${HVRL_SOURCE_DIR}/thirdparty/gtest/include")

        set(test_deps ${the_target} gtest_main gtest ${HVRL_EXTERN_LIBS})

	FILE(GLOB test_srcs "test/*.cpp")
        FILE(GLOB test_hdrs "test/*.h*")

        SOURCE_GROUP("Src" FILES ${test_srcs} )
        SOURCE_GROUP("Include" FILES ${test_hdrs})

	SET(the_target "${the_target}-test")
        add_executable(${the_target} ${test_srcs} ${test_hdrs})

IF(NOT WIN32)
        add_dependencies(${the_target} ${HVRL_EXTERN_LIBS} ${ARGN})
ENDIF()
        TARGET_LINK_LIBRARIES(${the_target} ${HVRL_EXTERN_LIBS}  ${test_deps})


	INSTALL(TARGETS ${the_target}
     		RUNTIME DESTINATION ${HVRL_BIN_INSTALL_PATH}/test
    		ARCHIVE DESTINATION ${HVRL_LIB_INSTALL_PATH})

endmacro()


macro(create_module name)

SET(HVRL_INCLUDE_DIRS ${HVRL_INCLUDE_DIRS} "${CMAKE_CURRENT_SOURCE_DIR}/include" CACHE INTERNAL "")
SET(namelocal ${name})
IF("${name}" MATCHES "_gold")
SET(gold "gold")
STRING(REGEX REPLACE "_gold" "" namelocal ${name})
set(name ${namelocal})
ELSE()
SET(gold "")
ENDIF()

PROJECT(HVRL_${namelocal})

SET(the_target "hvrl_${namelocal}")

INCLUDE_DIRECTORIES("${CMAKE_CURRENT_SOURCE_DIR}/include"
		    "${CMAKE_CURRENT_SOURCE_DIR}/src"
		    "${CMAKE_CURRENT_BINARY_DIR}")

SET(InDi "")
#include directories of the
    foreach(d ${ARGN})
        if(d MATCHES "hvrl_")
            string(REPLACE "hvrl_" "${HVRL_SOURCE_DIR}/modules/" d_dir ${d})
            if (EXISTS "${d_dir}/include")
		SET(InDi ${InDi} "${d_dir}/include")
		INCLUDE_DIRECTORIES("${d_dir}/include")
            endif()
        endif()
    endforeach()

FILE(GLOB lib_srcs "src${gold}/*.cpp")
FILE(GLOB lib_hdrs "include/hvrl/${namelocal}/*.hpp")
FILE(GLOB lib_hdrs_in "include/hvrl/${namelocal}/*.hpp.in")

foreach(d ${lib_hdrs_in})
	string(REPLACE ".in" "" lib_hdrs_in_output ${d})
	configure_file(${d} ${lib_hdrs_in_output})
	set(lib_hdrs ${lib_hdrs} ${lib_hdrs_in_output})
endforeach()

foreach(d ${lib_hdrs})
        if(d MATCHES "qt")
		SET(QT_HDRS ${QT_HDRS} "${d}")
	else()
		SET(CPP_HDRS ${CPP_HDRS} "${d}")

            endif()
    endforeach()

if(DEFINED QT_HDRS)
qt4_wrap_cpp(FOO_MOC_SRCS ${QT_HDRS})
endif()

SOURCE_GROUP("Src" FILES ${lib_srcs})
SOURCE_GROUP("Include" FILES ${lib_hdrs})

IF(NOT "${lib_srcs}" STREQUAL "")

IF(HVRL_BUILD_SHARED_LIBS)
        IF(MSVC)
            set_target_properties(${the_target} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
        ELSE()
            add_definitions(-DCVAPI_EXPORTS)        
        ENDIF()
	ADD_LIBRARY(${the_target} ${lib_srcs} ${CPP_HDRS} ${FOO_MOC_SRCS})
ELSE()
	ADD_LIBRARY(${the_target} STATIC ${lib_srcs} ${CPP_HDRS} ${FOO_MOC_SRCS})
ENDIF()

TARGET_LINK_LIBRARIES(${the_target} ${HVRL_EXTERN_LIBS} ${HVRL_LIBS})

SET_TARGET_PROPERTIES(${the_target} PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY ${HVRL_LIB_INSTALL_PATH}
        RUNTIME_OUTPUT_DIRECTORY ${HVRL_BIN_INSTALL_PATH}
        INSTALL_NAME_DIR lib)

INSTALL(TARGETS ${the_target}
        RUNTIME DESTINATION ${HVRL_BIN_INSTALL_PATH}
        LIBRARY DESTINATION ${HVRL_LIB_INSTALL_PATH} COMPONENT main
        ARCHIVE DESTINATION ${HVRL_LIB_INSTALL_PATH} COMPONENT main)

SET(HVRL_LIBS ${HVRL_LIBS} ${the_target} CACHE INTERNAL "")
ENDIF(NOT "${lib_srcs}" STREQUAL "")

INSTALL(FILES ${lib_hdrs} DESTINATION ${HVRL_INCLUDE_INSTALL_PATH}/hvrl/${namelocal}/)

IF(HVRL_BUILD_TEST AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test)
	create_test(${namelocal} ${ARGN})
ENDIF()

endmacro()



